package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.WebVisitMapper;
import com.itheima.pojo.WebVisit;
import com.itheima.service.WebVisitService;
import com.itheima.vo.WebVisitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class WebVisitServiceImpl implements WebVisitService {
    @Autowired
    private WebVisitMapper webVisitMapper;
    @Override
    public PageInfo<WebVisit> list(WebVisitVo webVisitVo) {
         //判断前端是否传了当前页和页面大小
        if (webVisitVo.getCurrentPage()!=null&&webVisitVo.getPageSize()!=null){
            PageHelper.startPage(webVisitVo.getCurrentPage(),webVisitVo.getPageSize());
        }
        //创建条件对象
        Example example = new Example(WebVisit.class);
        //创建条件添加对象
        Example.Criteria criteria = example.createCriteria();
        if (webVisitVo.getKeyword()!=null&&!"".equals(webVisitVo.getKeyword())){
            //关键词与这些属性相似的话 就添加模糊查询条件
            criteria.orLike("behavior","%"+webVisitVo.getKeyword()+"%");
            criteria.orLike("ip","%"+webVisitVo.getKeyword()+"%");
            criteria.orLike("os","%"+webVisitVo.getKeyword()+"%");
        }
        //判断开始时间是否为空
        if (webVisitVo.getStartTime()!=null){
            //不为空就添加条件
            // 将字符串转换成Date类型
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                criteria.andGreaterThanOrEqualTo("createTime", sdf.parse(webVisitVo.getStartTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        List<WebVisit> webVisits = webVisitMapper.selectByExample(example);
        return new PageInfo<WebVisit>(webVisits);
    }

}
